<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kon extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->library('form_validation');
	//if ($this->session->userdata('isLoggedIn')){
            //$menu['isLoggedIn'] = true;
        //}else {
        //	redirect('site');
        //}
	}

	public function index(){
	
		
		$this->load->model('konsumen','',true);
		$data['konsumen'] = $this->konsumen->get_all();
		
		$this->load->view('header');
		$menu['isLoggedIn'] = true;
		$this->load->view('menu',$menu);
		$this->load->view('konsumen',$data);
		$this->load->view('footer');
	}

	public function insert(){
		$data = array(
				'id_konsumen' 			=> $this->input->post('id_konsumen'),
				'nama' 					=> $this->input->post('nama'),
				'identitas' 			=> $this->input->post('identitas'),
				'alamat'				=> $this->input->post('alamat'),
				'kecamatan'				=> $this->input->post('kecamatan'),
				'kota'					=> $this->input->post('kota'),
				'telp'					=> $this->input->post('telp'),
				'cabang'				=> $this->input->post('cabang'),
			);
		$this->load->model('konsumen','',true);
		$this->form_validation->set_rules('id_konsumen', 'id_konsumen', 'trim|required|is_unique[konsumen.id_konsumen]');
		if ($this->form_validation->run() == FALSE) {
			$menu['error'] = true;
			$this->load->model('konsumen','',true);
			$data['konsumen'] = $this->konsumen->get_all();
			$this->load->view('header');
			$this->load->view('menu',$menu);
			$this->load->view('konsumen',$data);
			$this->load->view('footer');
		} else {
			$this->konsumen->insert($data);
			redirect('kon');
		}	
	}

	public function delete($data){
		$this->load->model('konsumen','',true);
		$this->konsumen->delete($data);
		redirect('kon');
	}

	public function edit($id){
		$this->load->model('konsumen','',true);
		$data['detail'] = $this->konsumen->get_by_id($id);
		$data['konsumen'] = $this->konsumen->get_all();
		//$menu['m_jenis_barang'] = $this->m_jenis_barang->get_all();
		$this->load->view('header');
		$menu['isLoggedIn'] = true;
		$this->load->view('menu',$menu);
		$this->load->view('konsumen',$data);
		$this->load->view('footer');
	}

	public function update(){
		$data = array(
				'id_konsumen' 			=> $this->input->post('id_konsumen'),
				'nama' 					=> $this->input->post('nama'),
				'identitas' 			=> $this->input->post('identitas'),
				'alamat'				=> $this->input->post('alamat'),
				'kecamatan'				=> $this->input->post('kecamatan'),
				'kota'					=> $this->input->post('kota'),
				'telp'					=> $this->input->post('telp'),
				'cabang'				=> $this->input->post('cabang')
			);
		$id = $this->input->post('id_konsumen');
		$this->load->model('konsumen','',true);
		$this->form_validation->set_rules('nama', 'nama', 'trim|required');
		if ($this->form_validation->run() == FALSE) {
			$menu['error'] = true;
			$this->load->model('konsumen','',true);
			$data['konsumen'] = $this->konsumen->get_all();
			$this->load->view('header');
			$this->load->view('menu',$menu);
			$this->load->view('konsumen',$data);
			$this->load->view('footer');
		} else {
			$this->konsumen->update($id,$data);
			redirect('kon');
		}
		
	}
}
